package easyway.trainapp.webframework.web;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 * 
 * @author caiwei 内含验证方法的后台bean
 */
public class ValidatorBB {
	private Pattern p;
	private Matcher m;
	private UIInput indivpass;

	// 电子邮箱验证器
	public void validateEmail(FacesContext context, UIComponent toValidate,
			Object value) {
		String email = (String) value;
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		p = Pattern.compile(regex);
		m = p.matcher(email);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("电子邮箱格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 中文用户名验证器
	public void validatecname(FacesContext context, UIComponent toValidate,
			Object value) {
		String usercname = (String) value;
		String regex = "[\u4e00-\u9fa5]";
		p = Pattern.compile(regex);
		m = p.matcher(usercname);
		if (!m.find() || usercname.length() > 6) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("请输入中文");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 手机号码验证器
	public void validatemobile(FacesContext context, UIComponent toValidate,
			Object value) {
		String mobilephone = (String) value;
		String regex = "^(13[0-9]|15[0|3|6|8|9])\\d{8}$";
		p = Pattern.compile(regex);
		m = p.matcher(mobilephone);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("手机号格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 用户名或密码格式验证器
	public void validateusrname(FacesContext context, UIComponent toValidate,
			Object value) {
		String username = (String) value;
		String regex = "^\\w+$";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("用户名格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 特殊符号验证器
	public void validatesymbol(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^\u4E00-\u9FA5|a-z|A-Z|0-9]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("不能输入特殊字符");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 邮政编码验证器
	public void validatepostcode(FacesContext context, UIComponent toValidate,
			Object value) {
		String username = (String) value;
		String regex = "[1-9]\\d{5}(?!\\d)";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("邮政编码格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 网址验证器
	public void validateurl(FacesContext context, UIComponent toValidate,
			Object value) {
		String username = (String) value;
		String regex = "[a-zA-z]+://[^\\s]*";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("url格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 固定电话验证器
	public void validatephone(FacesContext context, UIComponent toValidate,
			Object value) {
		String username = (String) value;
		String regex = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式为:区号-电话号");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 验证individualDetail中的密码一致
	public void validateindivDepassword(FacesContext context,
			UIComponent toValidate, Object value) {
		String twopassword = (String) value;
		String pass = (String) (indivpass.getLocalValue() == null ? ""
				: indivpass.getLocalValue());
		if (!pass.equals(twopassword)) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("密码不一致");
			context.addMessage(toValidate.getClientId(context), message);
		}

	}

	// 中文和字母得验证
	public void chinaLetterValidate(FacesContext context,
			UIComponent component, Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^\u4E00-\u9FA5|a-z|A-Z]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入字母、汉字");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 中文和字母和数字得验证
	public void chinaLetNumValidate(FacesContext context,
			UIComponent component, Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^\u4E00-\u9FA5|a-z|A-Z|0-9]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("只能输入字母、汉字、数字");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 字母和数字得验证
	public void letterNumValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^a-z|A-Z|0-9]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find() || cnLetter.length() < 6 || cnLetter.length() > 12) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("格式不正确，请输入6至12位的字母或数字");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 数字得验证
	public void numValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^0-9]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入数字");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 汉字得验证
	public void chinaValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^\u4E00-\u9FA5]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入汉字");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 字母得验证
	public void letterValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = "[a-zA-Z]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入英文");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 字母得验证
	public void strNullValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = ".+[^a-z|A-Z]+";
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入英文");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 英文名得验证-zhangqihang
	public void enNameValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String cnLetter = (String) obj;
		String regex = "^([a-zA-Z]+\\s*)+$";// 就验证字母跟空格
		p = Pattern.compile(regex);
		m = p.matcher(cnLetter);
		if (!m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("请输入正确英文名");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// mac地址得验证-zhangqihang
	public void strMacValidate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String mac = (String) obj;
		String regex = "^(([A-Z\\d]{2}:){5}[A-Z\\d]{2})$";
		p = Pattern.compile(regex);
		m = p.matcher(mac);
		if (!m.find()) {
			((UIInput) component).setValid(false);
			FacesMessage message = new FacesMessage("终端序列号不正确");
			context.addMessage(component.getClientId(context), message);

		}
	}

	// 固化或手机验证-zhangqihang
	public void phonevalidate(FacesContext context, UIComponent toValidate,
			Object value) {
		String username = (String) value;
		String regex = "\\d{3}-\\d{8}|\\d{4}-\\d{7}|(13)|(15)|(18)[0-9]{9}";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式为:区号-电话号");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 证件类型号码-guozhuang
	public void paperNumber(FacesContext context, UIComponent toValidate,
			Object value) {
		
		String paperNumber = (String) value;
		String regex15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
		String regex18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$";
		String regex = "(P\\d{7}|G\\d{8})";
		if (paperNumber.length() == 8) {
			p = Pattern.compile(regex);
			m = p.matcher(paperNumber);
			if (!m.find()) {
				((UIInput) toValidate).setValid(false);
				FacesMessage message = new FacesMessage("格式不正确");
				context.addMessage(toValidate.getClientId(context), message);

			}
		} else if (paperNumber.length() == 9) {
			p = Pattern.compile(regex);
			m = p.matcher(paperNumber);
			if (!m.find()) {
				((UIInput) toValidate).setValid(false);
				FacesMessage message = new FacesMessage("格式不正确");
				context.addMessage(toValidate.getClientId(context), message);
			}
		} else if (paperNumber.length() == 15) {
			p = Pattern.compile(regex15);
			m = p.matcher(paperNumber);
			if (!m.find()) {
				((UIInput) toValidate).setValid(false);
				FacesMessage message = new FacesMessage("格式不正确");
				context.addMessage(toValidate.getClientId(context), message);
			}
		} else if (paperNumber.length() == 18) {
			p = Pattern.compile(regex18);
			m = p.matcher(paperNumber);
			if (!m.find()) {
				((UIInput) toValidate).setValid(false);
				FacesMessage message = new FacesMessage("格式不正确");
				context.addMessage(toValidate.getClientId(context), message);
			}
		} else if (paperNumber.length() != 15 && paperNumber.length() != 18
				&& paperNumber.length() != 9 && paperNumber.length() != 8) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}

	}

	// 护照号码-guozhuang
	public void passPort(FacesContext context, UIComponent toValidate,
			Object value) {

		String passport = (String) value;
		String regex = "(P\\d{7}|G\\d{8})";

		p = Pattern.compile(regex);
		m = p.matcher(passport);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 邮政编码验证器三列--guozhuang
	public void validatepostcode01(FacesContext context,
			UIComponent toValidate, Object value) {
		String username = (String) value;
		String regex = "[1-9]\\d{5}(?!\\d)";
		p = Pattern.compile(regex);
		m = p.matcher(username);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// 手机号码验证器三列--guozhuang
	public void validatemobile01(FacesContext context, UIComponent toValidate,
			Object value) {
		String mobilephone = (String) value;
		String regex = "^(13[0-9]|15[0|3|6|8|9])\\d{8}$";
		p = Pattern.compile(regex);
		m = p.matcher(mobilephone);
		if (!m.find()) {
			((UIInput) toValidate).setValid(false);
			FacesMessage message = new FacesMessage("格式不正确");
			context.addMessage(toValidate.getClientId(context), message);
		}
	}

	// getter and setter
	public UIInput getIndivpass() {
		return indivpass;
	}

	public void setIndivpass(UIInput indivpass) {
		this.indivpass = indivpass;
	}

}
